package com.xywl.escort.view.address;

import com.xywl.escort.bean.AddrListResp;
import com.xywl.escort.bean.AddrReq;
import com.xywl.escort.bean.BaseReq;
import com.xywl.escort.bean.BaseResp;
import com.xywl.escort.bean.DelAddressReq;
import com.xywl.escort.bean.GetAddressListReq;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

/**
 * Created by 32422 on 2017/10/24.
 */

public class ManageAddressPresenter implements ManageAddressContract.Presenter {
    private ManageAddressContract.View mView;
    private ApiServer mApiServer;

    @Inject
    ManageAddressPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void takeView(ManageAddressContract.View view) {
        this.mView = view;
    }

    @Override
    public void unSubscribe() {
        mView = null;
    }

    @Override
    public void loadAddressList(int pageNum) {
        GetAddressListReq req = new GetAddressListReq();
        req.isDefault = 2;
        req.pageNum = pageNum;
        mApiServer.addrList(req)
                .compose(RxUtil.<AddrListResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<AddrListResp>bindToLife())
                .subscribe(new BaseSubscriber<AddrListResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadAddressListFailure();
                    }

                    @Override
                    public void onNext(AddrListResp resp) {
                        if (null != resp && resp.isOk()) {
                            mView.loadAddressListSuccess(resp);
                        } else {
                            mView.loadAddressListFailure();
                        }
                    }
                });
    }

    @Override
    public void updateAddressStatus(AddrReq req) {
        mApiServer.addrUpdate(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.updateAddressFailure("操作失败");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.updateAddressSuccess(resp);
                            } else {
                                mView.updateAddressFailure(resp.msg);
                            }
                        } else {
                            mView.updateAddressFailure("操作失败");
                        }
                    }
                });
    }

    @Override
    public void delAddress(DelAddressReq req) {
        mApiServer.addrDel(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.delAddressFailure("操作失败");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.delAddressSuccess(resp);
                            } else {
                                mView.delAddressFailure(resp.msg);
                            }
                        } else {
                            mView.delAddressFailure("操作失败");
                        }
                    }
                });
    }
}
